package cn.sciento.transfer.domain.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import cn.sciento.core.exception.CommonException;import io.choerodon.mybatis.annotation.ModifyAudit;
import io.choerodon.mybatis.annotation.VersionAudit;
import io.choerodon.mybatis.domain.AuditDomain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import cn.sciento.boot.platform.lov.annotation.LovValue;
import cn.sciento.starter.keyencrypt.core.Encrypt;

@ApiModel("事件错误监控")
@VersionAudit
@ModifyAudit
@Table(name = "hdtt_event_error")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EventError extends AuditDomain {
  public static final String ENCRYPT_KEY = "cn.sciento.transfer.domain.entity.EventError";
  
  public static final String FIELD_EVENT_ID = "eventId";
  
  public static final String FIELD_EVENT_TYPE = "eventType";
  
  public static final String FIELD_SOURCE_SERVICE = "sourceService";
  
  public static final String FIELD_SOURCE_TABLE = "sourceTable";
  
  public static final String FIELD_SOURCE_KEY_ID = "sourceKeyId";
  
  public static final String FIELD_SOURCE_TENANT_ID = "sourceTenantId";
  
  public static final String FIELD_TARGET_SERVICE = "targetService";
  
  public static final String FIELD_PRODUCE_TIME = "produceTime";
  
  public static final String FIELD_MESSAGES = "messages";
  
  public static final String FIELD_RETRY_TIMES = "retryTimes";
  
  public static final String FIELD_ERROR_TYPE = "errorType";
  
  public static final String FIELD_ERROR_TIME = "errorTime";
  
  public static final String FIELD_ERROR_TEXT = "errorText";
  
  public static final String FIELD_PROCESS_STATUS = "processStatus";
  
  public static final String FIELD_PROCESS_MSG = "processMsg";
  
  public static final String FIELD_PROCESS_TIME = "processTime";
  
  @Encrypt
  @ApiModelProperty("表ID，主键")
  @Id
  private Long eventErrorId;
  
  @ApiModelProperty("全局ID")
  private Long eventId;
  
  @ApiModelProperty("事件类型")
  @NotBlank
  @LovValue(lovCode = "HDTT.EVENT_TYPE", meaningField = "eventTypeMeaning")
  private String eventType;
  
  @ApiModelProperty("服务名")
  @NotBlank
  private String sourceService;
  
  @ApiModelProperty("处理表，也是消息TOPIC")
  @NotBlank
  private String sourceTable;
  
  @ApiModelProperty("处理数据ID")
  private Long sourceKeyId;
  
  @ApiModelProperty("处理数据租户ID")
  private Long sourceTenantId;
  
  @ApiModelProperty("消息产生时间")
  @NotNull
  private Date produceTime;
  
  @ApiModelProperty("消息内容")
  private String messages;
  
  @ApiModelProperty("重试次数")
  @NotNull
  private Long retryTimes;
  
  @ApiModelProperty("出错类型，参考HDTT.EVENT_ERR_TYPE")
  @NotBlank
  @LovValue(lovCode = "HDTT.EVENT_ERR_TYPE", meaningField = "errorTypeMeaning")
  private String errorType;
  
  @ApiModelProperty("出错时间")
  private Date errorTime;
  
  @ApiModelProperty("错误消息")
  private String errorText;
  
  @ApiModelProperty("处理状态，参考：HDTT.EVENT_PROCESS_STATUS")
  @NotBlank
  @LovValue(lovCode = "HDTT.EVENT_PROCESS_STATUS", meaningField = "processStatusMeaning")
  private String processStatus;
  
  @ApiModelProperty("处理消息")
  private String processMsg;
  
  @ApiModelProperty("处理时间")
  private Date processTime;
  
  @ApiModelProperty("数据源Id")
  private Long targetDsId;
  
  @ApiModelProperty("数据源编码")
  private String targetTable;
  
  @ApiModelProperty("数据源DB编码")
  private String targetDb;
  
  @ApiModelProperty("目标服务名")
  private String targetService;
  
  @ApiModelProperty("数据源")
  private String targetDs;
  
  @Transient
  private String processStatusMeaning;
  
  @Transient
  private String errorTypeMeaning;
  
  @Transient
  private String sourceTenantName;
  
  @Transient
  private String eventTypeMeaning;
  
  @Encrypt
  @Transient
  private Long producerConfigId;
  
  @Transient
  private Date errorTimeFrom;
  
  @Transient
  private Date errorTimeTo;
  
  @Transient
  private String errorEventId;
  
  @Transient
  private Integer checkCompleteFlag;
  
  public void validStatus() {
    if ("R".equals(this.processStatus))
      throw new CommonException("error.error_event_is_running");
    if ("S".equals(this.processStatus))
      throw new CommonException("error.error_event_is_completed");
  }
  
  public void generateId() {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(this.eventId);
    if (this.sourceKeyId != null)
      stringBuilder.append(this.sourceKeyId); 
    if (this.targetService != null)
      stringBuilder.append(this.targetService); 
    if (this.targetDb != null)
      stringBuilder.append(this.targetDb); 
    if (this.targetTable != null)
      stringBuilder.append(this.targetTable); 
    this.errorEventId = stringBuilder.toString();
  }
  
  public static EventError getInstance(EventSend eventSend, String errorType, String errorMsg) {
    EventError eventError = new EventError();
    eventError.setEventId(eventSend.getEventId());
    eventError.setEventType(eventSend.getEventType());
    eventError.setMessages(eventSend.getMessages());
    eventError.setProduceTime(eventSend.getProduceTime());
    eventError.setSourceTable(eventSend.getSourceTable());
    eventError.setSourceService(eventSend.getSourceService());
    eventError.setErrorTime(new Date());
    eventError.setErrorText(errorMsg);
    eventError.setErrorType(errorType);
    eventError.setRetryTimes(Long.valueOf(0L));
    return eventError;
  }
  
  public Long getEventId() {
    return this.eventId;
  }
  
  public void setEventId(Long eventId) {
    this.eventId = eventId;
  }
  
  public String getEventType() {
    return this.eventType;
  }
  
  public void setEventType(String eventType) {
    this.eventType = eventType;
  }
  
  public String getSourceService() {
    return this.sourceService;
  }
  
  public void setSourceService(String sourceService) {
    this.sourceService = sourceService;
  }
  
  public String getSourceTable() {
    return this.sourceTable;
  }
  
  public void setSourceTable(String sourceTable) {
    this.sourceTable = sourceTable;
  }
  
  public Long getSourceKeyId() {
    return this.sourceKeyId;
  }
  
  public void setSourceKeyId(Long sourceKeyId) {
    this.sourceKeyId = sourceKeyId;
  }
  
  public Long getSourceTenantId() {
    return this.sourceTenantId;
  }
  
  public void setSourceTenantId(Long sourceTenantId) {
    this.sourceTenantId = sourceTenantId;
  }
  
  public String getTargetService() {
    return this.targetService;
  }
  
  public void setTargetService(String targetService) {
    this.targetService = targetService;
  }
  
  public Date getProduceTime() {
    return this.produceTime;
  }
  
  public void setProduceTime(Date produceTime) {
    this.produceTime = produceTime;
  }
  
  public String getMessages() {
    return this.messages;
  }
  
  public void setMessages(String messages) {
    this.messages = messages;
  }
  
  public Long getRetryTimes() {
    return this.retryTimes;
  }
  
  public void setRetryTimes(Long retryTimes) {
    this.retryTimes = retryTimes;
  }
  
  public String getErrorType() {
    return this.errorType;
  }
  
  public void setErrorType(String errorType) {
    this.errorType = errorType;
  }
  
  public Date getErrorTime() {
    return this.errorTime;
  }
  
  public void setErrorTime(Date errorTime) {
    this.errorTime = errorTime;
  }
  
  public String getErrorText() {
    return this.errorText;
  }
  
  public void setErrorText(String errorText) {
    this.errorText = errorText;
  }
  
  public String getProcessStatus() {
    return this.processStatus;
  }
  
  public void setProcessStatus(String processStatus) {
    this.processStatus = processStatus;
  }
  
  public String getProcessMsg() {
    return this.processMsg;
  }
  
  public void setProcessMsg(String processMsg) {
    this.processMsg = processMsg;
  }
  
  public Date getProcessTime() {
    return this.processTime;
  }
  
  public void setProcessTime(Date processTime) {
    this.processTime = processTime;
  }
  
  public String getTargetDb() {
    return this.targetDb;
  }
  
  public void setTargetDb(String targetDb) {
    this.targetDb = targetDb;
  }
  
  public Long getTargetDsId() {
    return this.targetDsId;
  }
  
  public void setTargetDsId(Long targetDsId) {
    this.targetDsId = targetDsId;
  }
  
  public String getTargetTable() {
    return this.targetTable;
  }
  
  public void setTargetTable(String targetTable) {
    this.targetTable = targetTable;
  }
  
  public String getErrorTypeMeaning() {
    return this.errorTypeMeaning;
  }
  
  public void setErrorTypeMeaning(String errorTypeMeaning) {
    this.errorTypeMeaning = errorTypeMeaning;
  }
  
  public String getProcessStatusMeaning() {
    return this.processStatusMeaning;
  }
  
  public void setProcessStatusMeaning(String processStatusMeaning) {
    this.processStatusMeaning = processStatusMeaning;
  }
  
  public String getEventTypeMeaning() {
    return this.eventTypeMeaning;
  }
  
  public void setEventTypeMeaning(String eventTypeMeaning) {
    this.eventTypeMeaning = eventTypeMeaning;
  }
  
  public Long getProducerConfigId() {
    return this.producerConfigId;
  }
  
  public void setProducerConfigId(Long producerConfigId) {
    this.producerConfigId = producerConfigId;
  }
  
  public String getTargetDs() {
    return this.targetDs;
  }
  
  public void setTargetDs(String targetDs) {
    this.targetDs = targetDs;
  }
  
  public Date getErrorTimeFrom() {
    return this.errorTimeFrom;
  }
  
  public void setErrorTimeFrom(Date errorTimeFrom) {
    this.errorTimeFrom = errorTimeFrom;
  }
  
  public Date getErrorTimeTo() {
    return this.errorTimeTo;
  }
  
  public void setErrorTimeTo(Date errorTimeTo) {
    this.errorTimeTo = errorTimeTo;
  }
  
  public String getErrorEventId() {
    return this.errorEventId;
  }
  
  public void setErrorEventId(String errorEventId) {
    this.errorEventId = errorEventId;
  }
  
  public Integer getCheckCompleteFlag() {
    return this.checkCompleteFlag;
  }
  
  public void setCheckCompleteFlag(Integer checkCompleteFlag) {
    this.checkCompleteFlag = checkCompleteFlag;
  }
  
  public Long getEventErrorId() {
    return this.eventErrorId;
  }
  
  public void setEventErrorId(Long eventErrorId) {
    this.eventErrorId = eventErrorId;
  }
  
  public String getSourceTenantName() {
    return this.sourceTenantName;
  }
  
  public void setSourceTenantName(String sourceTenantName) {
    this.sourceTenantName = sourceTenantName;
  }
}
